package com.example.idachuappone.cook.dialog;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.example.idachuappone.R;
import com.example.idachuappone.cook.entity.Area;
import com.example.idachuappone.cook.entity.Tags;
import com.example.idachuappone.utils.Utils;

public class DialogCookNav {

	class AnonymousClass_1 implements OnDismissListener {
		private final/* synthetic */ImageView val$img_cook_sort;

		AnonymousClass_1(ImageView imageView) {
			this.val$img_cook_sort = imageView;
		}

		@Override
		public void onDismiss() {
			this.val$img_cook_sort.setImageResource(R.drawable.icon_cook_nav_off);
		}
	}

	class AnonymousClass_2 implements OnClickListener {
		private final/* synthetic */CallBackTags val$callBackTags;

		AnonymousClass_2(CallBackTags callBackTags) {
			this.val$callBackTags = callBackTags;
		}

		@Override
		public void onClick(View v) {
			this.val$callBackTags.onDialogDismiss();
		}
	}

	class AnonymousClass_3 implements OnClickListener {
		private final/* synthetic */CallBackTags val$callBackTags;
		private final/* synthetic */int val$tempnum;

		AnonymousClass_3(CallBackTags callBackTags, int i) {
			this.val$callBackTags = callBackTags;
			this.val$tempnum = i;
		}

		@Override
		public void onClick(View v) {
			this.val$callBackTags.onClick(this.val$tempnum);
		}
	}

	class AnonymousClass_4 implements OnDismissListener {
		private final/* synthetic */ImageView val$img_cook_sort;

		AnonymousClass_4(ImageView imageView) {
			this.val$img_cook_sort = imageView;
		}

		@Override
		public void onDismiss() {
			this.val$img_cook_sort.setImageResource(R.drawable.icon_cook_nav_off);
		}
	}

	class AnonymousClass_5 implements OnClickListener {
		private final/* synthetic */CallBackTags val$callBackTags;

		AnonymousClass_5(CallBackTags callBackTags) {
			this.val$callBackTags = callBackTags;
		}

		@Override
		public void onClick(View v) {
			this.val$callBackTags.onDialogDismiss();
		}
	}

	class AnonymousClass_6 implements OnClickListener {
		private final/* synthetic */CallBackTags val$callBackTags;
		private final/* synthetic */int val$tempnum;

		AnonymousClass_6(CallBackTags callBackTags, int i) {
			this.val$callBackTags = callBackTags;
			this.val$tempnum = i;
		}

		@Override
		public void onClick(View v) {
			this.val$callBackTags.onClick(this.val$tempnum);
		}
	}

	public static interface CallBackTags {
		void onClick(int i);

		void onDialogDismiss();
	}

	public PopupWindow showTagsList(Context context, int screenWidth, List<Tags> list, int position,
			ImageView img_cook_sort, CallBackTags callBackTags) {
		View popupWindow = LinearLayout.inflate(context, R.layout.dialog_cook_tags, null);
		PopupWindow dialogTags = new PopupWindow(popupWindow);
		dialogTags.setOnDismissListener(new AnonymousClass_1(img_cook_sort));
		((LinearLayout) popupWindow.findViewById(R.id.ll_dismiss))
				.setOnClickListener(new AnonymousClass_2(callBackTags));
		LinearLayout ll_main = (LinearLayout) popupWindow.findViewById(R.id.ll_main);
		int x = 0;
		int index = 0;
		LinearLayout layout = null;
		while (index < list.size()) {
			int btnWidth;
			String temp = list.get(index).getName();
			if (x == 0) {
				layout = new LinearLayout(context);
				layout.setLayoutParams(new LayoutParams(-1, -2));
				layout.setPadding(0, Utils.dip2px(context, 10), 0, 0);
				btnWidth = (temp.length() * Utils.dip2px(context, 14)) + (Utils.dip2px(context, 15) * 2);
				x += (Utils.dip2px(context, 15) * 2) + btnWidth;
				ll_main.addView(layout);
			} else {
				btnWidth = (temp.length() * Utils.dip2px(context, 14)) + (Utils.dip2px(context, 15) * 2);
				x += Utils.dip2px(context, 10) + btnWidth;
			}
			if (x > screenWidth) {
				x = 0;
			} else {
				int tempnum = index;
				Button button = new Button(context);
				button.setLayoutParams(new LayoutParams(btnWidth, Utils.dip2px(context, 35)));
				button.setTextSize(14);
				if (position == index) {
					button.setBackgroundResource(2130837732);
					button.setTextColor(context.getResources().getColor(R.color.white));
				} else {
					button.setTextColor(context.getResources().getColor(R.color.color_two_lable_txt));
					button.setBackgroundResource(2130837847);
				}
				button.setText(list.get(index).getName());
				button.setOnClickListener(new AnonymousClass_3(callBackTags, tempnum));
				View textView = new TextView(context);
				textView.setLayoutParams(new LayoutParams(Utils.dip2px(context, 10), 35));
				layout.addView(button);
				layout.addView(textView);
				index++;
			}
		}
		ll_main.startAnimation(AnimationUtils.loadAnimation(context, 2130968579));
		dialogTags.setWidth(-1);
		dialogTags.setHeight(-1);
		dialogTags.setBackgroundDrawable(new BitmapDrawable());
		return dialogTags;
	}

	public PopupWindow showAddressList(Context context, int screenWidth, List<Area> list, int position,
			ImageView img_cook_sort, CallBackTags callBackTags) {
		View popupWindow = LinearLayout.inflate(context, R.layout.dialog_cook_tags, null);
		PopupWindow dialogTags = new PopupWindow(popupWindow);
		dialogTags.setOnDismissListener(new AnonymousClass_4(img_cook_sort));
		LinearLayout ll_main = (LinearLayout) popupWindow.findViewById(R.id.ll_main);
		((LinearLayout) popupWindow.findViewById(R.id.ll_dismiss))
				.setOnClickListener(new AnonymousClass_5(callBackTags));
		int x = 0;
		int index = 0;
		LinearLayout layout = null;
		while (index < list.size()) {
			int btnWidth;
			String temp = list.get(index).getName();
			if (x == 0) {
				layout = new LinearLayout(context);
				layout.setLayoutParams(new LayoutParams(-1, -2));
				layout.setPadding(0, Utils.dip2px(context, 10), 0, 0);
				btnWidth = (temp.length() * Utils.dip2px(context, 14)) + (Utils.dip2px(context, 15) * 2);
				x += (Utils.dip2px(context, 15) * 2) + btnWidth;
				ll_main.addView(layout);
			} else {
				btnWidth = (temp.length() * Utils.dip2px(context, 14)) + (Utils.dip2px(context, 15) * 2);
				x += Utils.dip2px(context, 10) + btnWidth;
			}
			if (x > screenWidth) {
				x = 0;
			} else {
				int tempnum = index;
				Button button = new Button(context);
				button.setLayoutParams(new LayoutParams(btnWidth, Utils.dip2px(context, 35)));
				button.setTextSize(14);
				if (position == index) {
					button.setBackgroundResource(2130837732);
					button.setTextColor(context.getResources().getColor(R.color.white));
				} else {
					button.setTextColor(context.getResources().getColor(R.color.color_two_lable_txt));
					button.setBackgroundResource(2130837847);
				}
				button.setText(list.get(index).getName());
				button.setOnClickListener(new AnonymousClass_6(callBackTags, tempnum));
				View textView = new TextView(context);
				textView.setLayoutParams(new LayoutParams(Utils.dip2px(context, 10), 35));
				layout.addView(button);
				layout.addView(textView);
				index++;
			}
		}
		ll_main.startAnimation(AnimationUtils.loadAnimation(context, 2130968579));
		dialogTags.setWidth(-1);
		dialogTags.setHeight(-1);
		dialogTags.setBackgroundDrawable(new BitmapDrawable());
		return dialogTags;
	}
}
